package com.flextribe.display
{
	import flash.events.MouseEvent;
	
	public class TribeButton extends TribeSprite{
		private var normal:TribeAsset;
		private var over:TribeAsset;
		private var down:TribeAsset;
		
		private var normalClass:Class;
		private var overClass:Class;
		private var downClass:Class;
		
		public override function set enabled(value:Boolean):void{
			mouseChildren = 
			mouseEnabled =
			normal.mouseEnabled =  value;
			super.enabled = value;
		}
		
		public function TribeButton(nrml:Class, ovr:Class, dwn:Class)
		{
			normalClass = nrml;
			overClass = ovr;
			downClass = dwn;
			super(true);
		}
		override public function createChildren():void{
			normal = new TribeAsset(normalClass);
			addChild(normal);
			
			normal.addEventListener(MouseEvent.MOUSE_OVER, mouseOver, false, 0, true);
			normal.addEventListener(MouseEvent.MOUSE_OUT, mouseOut, false, 0, true);
			normal.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);
			
			over = new TribeAsset(overClass);
			addChild(over);
			over.visible = false;
			
			down = new TribeAsset(downClass);
			addChild(down);
			down.visible = false;
			
			over.mouseChildren = 
			over.mouseEnabled =
			down.mouseChildren = 
			down.mouseEnabled =
								false;
		}
		private function mouseOver(event:MouseEvent):void{
			over.visible = true;
		}
		private function mouseOut(event:MouseEvent):void{
			over.visible = false;
		}
		private function mouseDown(event:MouseEvent):void{
			over.visible = false;
			down.visible = true;
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp, false, 0, true);
		}
		private function mouseUp(event:MouseEvent):void{
			down.visible = false;
			over.visible = this.enabled && normal.hitTestPoint(stage.mouseX, stage.mouseY);
		}
	}
}